package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.univ.po.LifeNews;
import com.g2room.myuall.util.DateUtil;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapJgaoxiaoData implements Runnable{
	private static String baseUrl = "http://xinwen.jgaoxiao.com/";
	private String soruce = "聚高校新闻";
	private static Map<String, String> urlMap = new HashMap<String, String>();
	static {
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-daxuesushe","大学宿舍");
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-daxueshitang","大学食堂");
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-xiaoyuanxinwen","校园新闻");
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-xueshenglvyou","学生旅游");
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-xiaohuaxiaocao","校花校草");
		urlMap.put("http://xinwen.jgaoxiao.com/daxueshenghuo-extra","其他");
	}
	private String pageUrl;
	private String type;
	public GrapJgaoxiaoData(String url, String type) {
		this.pageUrl = url;
		this.type = type;
	}
	public static void main(String[] args) throws Exception {
		for(String url : urlMap.keySet()) {
			GrapJgaoxiaoData gcd = new GrapJgaoxiaoData(url, urlMap.get(url));
			Thread thread = new Thread(gcd);
			thread.start();
		}
	}

	/**
	 * 获取头部导航菜单
	 * 
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getPage() {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(pageUrl);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(pageUrl);
			ge.setType("1");
			ge.setContent("获取链接错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}

		if (null == doc) return;
		List<String> urls = new ArrayList<String>();
		try {
			Elements els = doc.select(".pagelist a");
			if (els == null || els.size() == 0) return;
			String text = els.get(els.size() - 3).text();
			int pn = Integer.valueOf(text);
			for(int i =1; i< pn+1;i++) {
				urls.add(pageUrl + "?page=" + i);
			}
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(pageUrl);
			ge.setType("1");
			ge.setContent("获取内容错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		System.out.println(type + "本次抓取数据：" + urls.size());

		for (String lurl : urls) {
			getList(lurl);
		}
	}

	public void getList(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent("");
			ge.setDescription("获取List 链接错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		
		if(null == doc) return;
		Elements els = doc.select(".news_list_ul li ");
		if(null == els || els.size() == 0) return;
		Element tempEl;
		Elements tempEls;
		List<LifeNews> nis = new ArrayList<LifeNews>();
		for(Element liel : els) {
			tempEls = liel.select(".news_list_img img");
			LifeNews ni = new LifeNews();
			if(null != tempEls && tempEls.size() > 0) {
				tempEl = tempEls.first();
				ni.setCover(baseUrl + tempEl.attr("src").replace("./", ""));
				// 获取图片高度和宽度
			}
			
			tempEls = liel.select(".news_list_detail a");
			if(null == tempEls || tempEls.isEmpty()) {
				tempEls = liel.select(".news_list_detail_spe a");
			}
			if(null != tempEls && !tempEls.isEmpty()) {
				tempEl = tempEls.first();
				ni.setSourceUrl(tempEl.attr("href"));
				ni.setTitle(tempEl.text());
			}
			tempEls = liel.select(".comm_biaoqian a");
			if(null != tempEls && tempEls.size() > 0) {
				String tags = "";
				for(Element el1: tempEls) {
					if(tags.length() > 1) tags+=",";
					tags+=el1.text();
				}
				ni.setTags(tags);
			}
			ni.setTypeName(type);
			nis.add(ni);
		}
		for(LifeNews li : nis) {
			getDetail(li);
		}
		
		insert2NewsDb(nis);
	}
	public void getDetail(LifeNews li) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(li.getSourceUrl());
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(li.getSourceUrl());
			ge.setType("4");
			ge.setContent("");
			ge.setDescription("获取Detail 链接错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}

		try {
			Elements els = doc.select(".source .fl span");
			if(null == els || els.isEmpty()) return;
			if(els.size() == 2) {
				li.setSource(els.get(0).text());
				li.setPublishTimeStr(els.get(1).text());
				li.setPublishTime(DateUtil.string2Date(els.get(1).text(), "yyyy-MM-dd"));
			} else if(els.size() == 1) {
				li.setPublishTimeStr(els.get(0).text());
				li.setPublishTime(DateUtil.string2Date(els.get(0).text(), "yyyy-MM-dd"));
			}
			
			els = doc.select(".brief_news");
			if(null != els && !els.isEmpty()) {
				li.setSummary(els.first().text().replace("【摘要】", ""));
				els.remove();
			}
			// 判断是否有分页
			els = doc.select(".pagelist a");
			int pn = 0;
			if(null != els && !els.isEmpty()) {
				String pnstr = els.get(els.size() - 3).text();
				pn = Integer.valueOf(pnstr)+1;
			}
			els = doc.select(".article_content");
			if(null != els && !els.isEmpty()) {
				String content = "";
				if(pn==0) content = delLastEl(els);
				for(int i=2;i<pn;i++) {
					String con = getContent(li.getSourceUrl() + "?page=" + i);
					if(con == null) return;
					content += con;
				}
				li.setContent(content);
			}
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(li.getSourceUrl());
			ge.setType("5");
			ge.setContent("");
			ge.setDescription("获取Detail 内容错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
	}
	
	public String getContent(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("8");
			ge.setContent("");
			ge.setDescription("获取Detail 分页链接错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}

		try {
			Elements els = doc.select(".article_content");
			if(null != els && !els.isEmpty()) {
				return delLastEl(els);
			}
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("9");
			ge.setContent("");
			ge.setDescription("获取Detail 分页内容错误：" + e.getMessage());
			insert2ErrDb(ge);
			return "";
		}
		return null;
	}
	
	public String delLastEl(Elements els) {
		// 去掉最后一个编辑
		Element el = els.first().select("p").last();
		if(el.text().contains("聚高校新闻")) el.remove();
		return els.first().html();
	}
	
	public void insert2NewsDb(List<LifeNews> nis) {
		String sql = "insert into news_univ(cover_height,cover_width,title,author,source,cover,type_name,source_url,tags,summary,content,publish_time) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getNewsConn().prepareStatement(sql);
			for(LifeNews cc : nis) {
				ps.setInt(1, null == cc.getCoverHeight() ? 0:cc.getCoverHeight());
				ps.setInt(2, null == cc.getCoverWidth() ? 0:cc.getCoverWidth());
				ps.setString(3, cc.getTitle());
				ps.setString(4, cc.getAuthor());
				ps.setString(5, cc.getSource());
				ps.setString(6, cc.getCover());
				ps.setString(7, cc.getTypeName());
				ps.setString(8, cc.getSourceUrl());
				ps.setString(9, cc.getTags());
				ps.setString(10, cc.getSummary());
				ps.setString(11, cc.getContent());
				ps.setString(12, cc.getPublishTimeStr());
				ps.addBatch();
			}
			ps.executeBatch();
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(nis));
			ge.setDescription("insert错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {

				}
			}
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getNewsConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {

				}
			}
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		getPage();
	}
}
